package servlet;

import util.ParameterUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/**
 * @author 黄衡熙
 * @version 1.0
 */
@WebServlet("/findById")
public class showModifyEmpServlet extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        String id=req.getParameter("id");
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1/web?useUnicode=true&characterEncoding=utf-8","root","qq670892");
            String sql="select id,name,salary,gender,edu,hobbies from t_emp where id=?";
            ps=conn.prepareStatement(sql);
            ps.setInt(1, Integer.parseInt(id));
            rs=ps.executeQuery();
            if(rs.next()){
                out.println(" <!DOCTYPE html> ");
                out.println(" <html lang=\"en\"> ");
                out.println(" <head> ");
                out.println("     <meta charset=\"UTF-8\"> ");
                out.println("     <title>修改员工</title> ");
                out.println(" </head> ");
                out.println(" <body> ");
                out.println(" <h1>修改员工信息</h1> ");
                out.println(" <a href=\"findAll\">返回员工列表</a> ");
                out.println(" <hr> ");
                out.println(" <form action=\"modifyEmp\" method=\"post\"> ");
                out.println("     编号:<input type=\"text\" name=\"id\" value=\"" + rs.getInt("id") + "\" readonly><br> ");
                out.println("     姓名:<input type=\"text\" name=\"name\" value=\"" + rs.getString("name") + "\"><br> ");
                out.println("     工资:<input type=\"text\" name=\"salary\" value=\"" + rs.getDouble("salary") + "\"><br> ");
                out.println("     性别:<input type=\"radio\" name=\"gender\" value=\"man\" " + ("man".equals(rs.getString("gender")) ? "checked" : "") + ">男 ");
                out.println("     <input type=\"radio\" name=\"gender\" value=\"woman\" " + ("woman".equals(rs.getString("gender")) ? "checked" : "") + ">女<br> ");
                out.println("     学历: ");
                out.println("     <select name=\"edu\"> ");
                out.println("         <option value=\"-1\">请选择</option> ");
                out.println("         <option value=\"1\" " + ("1".equals(rs.getString("edu")) ? "selected" : "") + ">大专</option> ");
                out.println("         <option value=\"2\" " + ("2".equals(rs.getString("edu")) ? "selected" : "") + ">本科</option> ");
                out.println("         <option value=\"3\" " + ("3".equals(rs.getString("edu")) ? "selected" : "") + ">研究生</option> ");
                out.println("         <option value=\"4\" " + ("4".equals(rs.getString("edu")) ? "selected" : "") + ">博士生</option> ");
                out.println("     </select><br> ");
                out.println("     兴趣: ");
                out.println("     <input type=\"checkbox\" name=\"hobbies\" value=\"eat\" " + ParameterUtil.checkHobby(rs.getString("hobbies"), "eat") + ">吃饭 ");
                out.println("     <input type=\"checkbox\" name=\"hobbies\" value=\"sleep\" " + ParameterUtil.checkHobby(rs.getString("hobbies"), "sleep") + ">睡觉 ");
                out.println("     <input type=\"checkbox\" name=\"hobbies\" value=\"beat\" " + ParameterUtil.checkHobby(rs.getString("hobbies"), "beat") + ">打豆豆<br> ");
                out.println("     <input type=\"submit\" value=\"修改\"> ");
                out.println(" </form> ");
                out.println(" </body> ");
                out.println(" </html> ");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("未找到驱动");
        } catch (SQLException e) {
            throw new RuntimeException("数据访问异常");
        }finally {
            if(conn !=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException("释放异常");
                }
            }
            if(ps !=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    throw new RuntimeException("释放异常");
                }
            }
            if(rs !=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException("释放异常");
                }
            }
        }
    }
}